package com.xm_jd.controller.goods;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xm_jd.model.dao.NewDao;
import com.xm_jd.model.entitys.GoodsDetailVO;
import com.xm_jd.model.entitys.pingpai;
import com.xm_jd.model.entitys.shoop;
import com.xm_jd.utils.BaseServlet;

/**
 * Servlet implementation class one
 */
@WebServlet("/xq_body")
public class one extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public one() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		//璁剧疆瀛楃闆�
		request.setCharacterEncoding("utf-8");
		//鑾峰彇鍟嗗搧缂栧彿
		String spid = request.getParameter("shoop_id");
		Integer shoopid= 0;
		if(spid==null){
			String ys = request.getParameter("color");
			String rl = request.getParameter("rom");
			//根据颜色和容量查询对应的商品编号
			List<GoodsDetailVO> tmp = new NewDao().getShoopId(ys, rl);
			if(tmp.size()>0){
				//浠庢煡璇㈢殑闆嗗悎涓彇鍑哄晢鍝佺紪鍙�
				shoopid = tmp.get(0).getShoop_Id();
			}
		}else{
			shoopid = Integer.parseInt(spid);
		}
		//鏍规嵁缂栧彿鏌ヨ鍟嗗搧鐨勭畝鐭弿杩�,浠锋牸
		List<shoop> list = new NewDao().getselectAll(shoopid);
		shoop sp = list.get(0);
		
		//鏌ヨ鍑哄晢鍝佺殑閲嶉噺鍒拌鎯呴〉闈�
		List<GoodsDetailVO> list1 = new NewDao().getseletspAll(shoopid);
		GoodsDetailVO w = list1.get(0);
		
		//鏌ヨ閿�鍞殑灞炴�� 
		List<GoodsDetailVO> list2 = new NewDao().getseletxssxAll();
		
		//鏍规嵁閿�鍞睘鎬у悕锛屾煡璇㈠搴旂殑鍊硷紝瀛樺叆Map闆嗗悎,閿�鍞睘鎬у悕涓簁ey
		Map<String,List<GoodsDetailVO>> map = new HashMap<>();
		for (GoodsDetailVO g : list2) {
			List<GoodsDetailVO> tmp =  new NewDao().getAllXssx(sp.getShoop_Brand(), g.getXssx_id());
			map.put(g.getXssx_name(),tmp);
		}	
		//鏌ヨ鏌愬晢鍝佺殑鍥剧墖鐨勮矾寰�
		List<GoodsDetailVO> urllist = new NewDao().getselectUrl(shoopid);
		  
		//鏌ヨ鏌愬晢鍝佺殑棰滆壊
		List<GoodsDetailVO> c = new NewDao().getseletcolorsp(shoopid);
		GoodsDetailVO color = c.get(0);
		
		//鏌ヨ鏌愬晢鍝佺殑ROM
		List<GoodsDetailVO> r = new NewDao().getseletROMsp(shoopid);
		GoodsDetailVO rom = r.get(0);
		
		//鏌ヨ鏌愬晢鍝佺殑鍝佺墝鍚嶇О
		List<pingpai> p = new NewDao().getselectbrand(shoopid);
		pingpai brand=p.get(0);
		
		if(list.size()>0){
			request.setAttribute("sp", sp);
			request.setAttribute("w", w);
			request.setAttribute("list2", list2);
			request.setAttribute("map", map);
			request.setAttribute("urllist", urllist);
			request.setAttribute("color", color.getXssxz_zhi());
			request.setAttribute("rom", rom.getXssxz_zhi());
			request.setAttribute("brand", brand);
		}
		//request.getRequestDispatcher("jsps/xq/xq_body.jsp").forward(request, response);
		this.forward("xq/xq_body");
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
